package com.server.pojo;

import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.server.pojo.base.PojoBase;
import lombok.*;
import lombok.experimental.Accessors;
import org.hibernate.Hibernate;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.*;

@Getter
@Setter
@ToString

@Entity
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Table
public class Alarm extends PojoBase {
    Integer type;
    Integer deviceType;

    @Column(precision = 13, scale = 10)
    BigDecimal longitude;
    @Column(precision = 13, scale = 10)
    BigDecimal latitude;

    Boolean ring = true;
    Boolean inform = true;

    Long scope;
    String message;

    Long duration = 5 * 60 * 1000L;
    Long anYuanPushInterval = 5*1000L;

    @ManyToOne
    Unit unit;
    @ManyToOne
    MobileApp mobileApp;

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    @JSONField(format = "millis")
    Date alarmAt;

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    @JSONField(format = "millis")
    Date locationAt;

    @JsonIgnore
    @OneToMany(mappedBy = "nowAlarm",cascade = CascadeType.ALL)
    @ToString.Exclude
    List<MobileApp> nowMobileApps = new ArrayList<>();

    @JsonIgnore
    @OneToMany(mappedBy = "alarm",cascade = CascadeType.ALL)
    @ToString.Exclude
    List<MobileAppAlarm> mobileAppAlarms = new ArrayList<>();

    @JsonIgnore
    @OneToMany(mappedBy = "alarm",cascade = CascadeType.ALL)
    @ToString.Exclude
    List<AlarmMessage> alarmMessages = new ArrayList<>();


    /*是否过期*/
    public boolean isExpired(){
        return new Date().getTime() > alarmAt.getTime() + duration;
    }

    @PrePersist
    public void prePersist(){

    }

    @PreUpdate
    public void preUpdate(){
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        Alarm alarm = (Alarm) o;
        return getId() != null && Objects.equals(getId(), alarm.getId());
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
